<?php
namespace Home\Controller;
use Org\Net\Http;

/**
 * 文件操作接口
 */
class FileController extends MainController {

    /**
     * ceshi
     */
    public function index(){
        $this->display();
    }




    /****************************** 二维地块start **********************************/
    /**
     * 上传
     */
    public function addFile(){
        $info = $this -> fileUpload('LocationAttachment',"TwoMassif");        
        if($info['status']){
            $_POST['fileid'] = $info['id'];
            $this -> addRecord();
        }else{
            $this -> ferror($info['msg']);
        }
    }
    
    /*
     * 下载
     */
    public function downFile(){
        if(I('get.fileid')){
            $info = M('file') -> where(array('id'=>I('get.fileid'))) -> find();
            $file = $_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'.$info['allpath'];
            if(!file_exists($file)){
                $this -> error("文件不存在");
            }
            Http::download($file);
        }else{
            $this -> error("未指定文件");
        }
    }
    
    /**
     * 删除
     */
    public function delFile(){
        if(I('post.fileid')){
            $info = M('file') -> where(array('id'=>I('post.fileid'))) -> find();
            $file = $_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'.$info['allpath'];
            //unlink($file);
            M('file') -> where(array('id'=>I('post.fileid'))) -> save(array('is_delete'=>1));
            
            $this -> delRecord();
        }else{
            $this -> ferror("未指定文件");
        }
    }
    
    
    /**
     * 二维地块记录添加
     */
    public function addRecord(){
        if( I('post.smid') &&  I('post.fileid') ){
            $save = array(
                'fileid' => I('post.fileid'),
                'smid' => I('post.smid'),
                'addtime' => time(),
                'is_delete' => 0
            );
            M('record') -> add($save);
            //select
            $info = M('record as r') -> join(C('DB_PREFIX')."file as f on r.fileid=f.id") -> field('r.fileid,r.smid,f.*') -> where(array('r.smid'=>I('post.smid') , 'r.is_delete'=>0)) -> select();
            $this -> fsucces($info);
        }else{
            $this -> ferror("smid和fileid不能为空");
        }
    }
    
    /**
     * 二维地块记录删除
     */
    public function delRecord(){
        if( I('post.smid') ){
            $where['smid'] = I('post.smid');
            if(I('post.fileid'))
                $where['fileid'] = I('post.fileid');
            //
            M('record') -> where($where) -> save(array('is_delete'=>1));
            $this -> fsucces("记录删除成功");
        }else{
            $this -> ferror("smid不能为空");
        }
    }
    
    /**
     * 二维地块记录查询
     */
    public function selectRecord(){
        if( I('post.smid') ){
            $info = M('record as r') -> join(C('DB_PREFIX')."file as f on r.fileid=f.id") -> field('r.fileid,r.smid,f.*') -> where(array('r.smid'=>I('post.smid') , 'r.is_delete'=>0)) -> select();
            if($info)
                $this -> fsucces($info);
            else
                $this -> ferror("未查询到数据");
        }else{
            $this -> ferror("smid不能为空");
        }
    }
    /****************************** 二维地块end **********************************/
    





    /****************************** 三维模型库start **********************************/
    /**
     * 上传相关信息（包含文件）
     */
    public function addFileThreeModel(){
        $info = $this -> fileUploads('ThreeModelLibrary' , "ThreeModel");
        if($info['status']){
            $addInfo = array(
                'modelName' => I('post.modelName'),
                'model_file_id' => $info['list']['modelFile']['id'],
                'thum_file_id' => $info['list']['sltFile']['id'],
                'model_type' => I('post.modelType'),
                'ttime' => time(),
                'is_deleted' => 0
            );
            M('ThreeModel') -> add($addInfo);
            //返回查询的json数据
            $this -> selectThreeModel();
        }else{
            $this -> ferror($info['msg']);
        }
    }
    
    /*
     * 下载
     */
    public function downFileThreeModel(){
        if(I('get.id')){
            $info = M('ThreeModel') -> where(array('id'=>I('get.id'))) -> find();
            $ModelURL = U('downFile')."?fileid=".$info['model_file_id'];
            $ThumURL = U('downFile')."?fileid=".$info['thum_file_id'];
            echo '<script>window.open("'.$ModelURL.'");</script>';
            echo '<script>window.open("'.$ThumURL.'");</script>';
            /*
            $fileinfo['modelFile'] = M('file') -> where(array('id' => $info['model_file_id'])) -> find();
            $fileinfo['thumFile'] = M('file') -> where(array('id' => $info['thum_file_id'])) -> find();
            $fileModel = $_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'.$fileinfo['modelFile']['allpath'];
            $fileThum = $_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'.$fileinfo['thumFile']['allpath'];
            Http::download($fileModel);
            Http::download($fileThum);
            */
        }else{
            $this -> error("未指定文件");
        }
    }
    
    /**
     * 删除
     */
    public function delFileThreeModel(){
        if(I('post.id')){
            //
            M('ThreeModel') -> where(array('id'=>I('post.id'))) -> save(array('is_deleted'=>1));
            //
            $info = M('ThreeModel') -> where(array('id'=>I('post.id'))) -> find();
            M('file') -> where("`id` in ('{$info[model_file_id]}','{$info[thum_file_id]}') ") -> save(array('is_delete'=>1));
            //
            $this -> fsucces("删除成功");
        }else{
            $this -> ferror("未指定文件");
        }
    }
    
    /**
     * 三维模型库查询
     */
    public function selectThreeModel(){
        $info = M('ThreeModel') -> where(array('is_deleted'=>0)) -> order('id desc') -> select();
        if($info){
            foreach($info as $k => $v){
                $modefile = M('file') -> where(array('id'=>$v['model_file_id'])) -> find();
                $thumfile = M('file') -> where(array('id'=>$v['thum_file_id'])) -> find();
                //
                $info[$k]['modelFileName'] = $modefile['filename'];
                $info[$k]['modelFileSaveName'] = $modefile['savename'];
                $info[$k]['modelFileSavePath'] = $modefile['savepath'];
                $info[$k]['modelFileSaveAllpath'] = $modefile['allpath'];
                $info[$k]['modelFileSize'] = $modefile['size'];
                $info[$k]['modelFileExtend'] = $modefile['extend'];
                $info[$k]['modelFileType'] = $modefile['type'];
                //
                $info[$k]['modelSltName'] = $thumfile['filename'];
                $info[$k]['modelSltSaveName'] = $thumfile['savename'];
                $info[$k]['modelSltSavePath'] = $thumfile['savepath'];
                $info[$k]['modelSltSaveAllpath'] = $thumfile['allpath'];
                $info[$k]['modelSltSize'] = $thumfile['size'];
                $info[$k]['modelSltExtend'] = $thumfile['extend'];
                $info[$k]['modelSltType'] = $thumfile['type'];
                //
                $info[$k]['addtime'] = $v['ttime'];
                $info[$k]['addtimeshow'] = date('Y-m-d H:i:s' , $v['ttime']);
            }
            $this -> fsucces($info);
        }else{
            $this -> ferror("未查询到数据");
        }
    }
    


    /****************************** 三维模型库end **********************************/
    
}
